home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / bitmap.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  4KB  |  172 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "object.h"
  13. #include "paintop.h"
  14.  
  15. extern F_compound    objects;
  16.  
  17. extern int        errno;
  18. extern int        DEBUG;
  19. extern int        pointmarker_shown;
  20. extern int        compoundbox_shown;
  21.  
  22. extern char        *sys_errlist[];
  23. extern int        sys_nerr, errno;
  24.  
  25. write_bitmap(file_name)
  26. char    *file_name;
  27. {
  28.     FILE            *fp;
  29.     struct stat         file_status;
  30.     char             string[180];
  31.  
  32. #ifdef AMIGA
  33.     extern struct Screen    *Screen;
  34.     extern struct Window    *AnotherWindow;
  35.  
  36.     set_temp_cursor(&wait_cursor);
  37.  
  38.     sprintf(string,"Saving bitmap to file \"%s\". . .",file_name);
  39.     put_msg(string);
  40.  
  41.     if(SaveRPort(&Screen -> RastPort,&Screen -> ViewPort,AnotherWindow -> LeftEdge,AnotherWindow -> TopEdge,AnotherWindow -> Width,AnotherWindow -> Height,Screen -> Width,Screen -> Height,TRUE,file_name))
  42.         put_msg("Done.");
  43.     else
  44.         put_msg("Failed to save bitmap.");
  45.  
  46.     reset_cursor();
  47.  
  48.     return;
  49. #else
  50.     if (*file_name == 0) {
  51.         put_msg("No file");
  52.         return;
  53.         }
  54.     if (stat(file_name, &file_status) == 0) { /* file exists */
  55.         if (file_status.st_mode & S_IFDIR) {
  56.         put_msg("\"%s\" is a directory", file_name);
  57.         return;
  58.         }
  59.         if (file_status.st_mode & S_IWRITE) { /* writing is permitted */
  60.         if (file_status.st_uid != geteuid()) {
  61.             put_msg("\"%s\" permission is denied", file_name);
  62.             return;
  63.             }
  64.         else {
  65.             sprintf(string, "\"%s\" File exists. Please click the LEFT button to COMFIRM overwrite. To cancel, click the MIDDLE or RIGHT button.", file_name);
  66.             if (wmgr_confirm(canvas_swfd, string) != -1) {
  67.             put_msg("No write.");
  68.             return;
  69.             }
  70.             }
  71.         }
  72.         else {
  73.         put_msg("\"%s\" File is read only", file_name);
  74.         return;
  75.         }
  76.         }
  77.     else if (errno != ENOENT) return;  /* file does exist but stat fails */
  78.  
  79.     if ((fp = fopen(file_name, "w")) == NULL) {
  80.         blink_msg();
  81.         put_msg("Couldn't open file %s, %s", file_name, sys_errlist[errno]);
  82.         return;
  83.         }
  84.     else {
  85.         create_n_write_bitmap(fp);
  86.         }
  87. #endif
  88.     }
  89.  
  90. create_n_write_bitmap(fp)
  91. FILE    *fp;
  92. {
  93.     int     box, marker, xmin, ymin, xmax, ymax;
  94.     PIXRECT    bitmap, pw_pixrect, pw_prretained;
  95.     F_text    *t;
  96.  
  97.     /* Assume that there is at least one object */
  98.     compound_bound(&objects, &xmin, &ymin, &xmax, &ymax);
  99.  
  100.     if (DEBUG) {
  101.         draw_rectbox(xmin, ymin, xmax, ymax, INV_PAINT);
  102.         }
  103. #ifndef    X11
  104. #ifdef AMIGA
  105. /*    fprintf(stderr,"n_write_bitmap\n");_abort(-1);*/
  106. #else
  107.     bitmap = mem_create(xmax-xmin+1, ymax-ymin+1, 1);
  108.     pw_pixrect = canvas_pixwin->pw_pixrect;
  109.     pw_prretained = canvas_pixwin->pw_prretained;
  110.     canvas_pixwin->pw_pixrect = canvas_pixwin->pw_prretained = bitmap;
  111.     translate_compound(&objects, -xmin, -ymin);
  112.     marker = pointmarker_shown;
  113.     pointmarker_shown = 0;
  114.     box = compoundbox_shown;
  115.     compoundbox_shown = 0;
  116.     pw_batch_on(canvas_pixwin);
  117.     redisplay_arcobject(objects.arcs);
  118.     redisplay_compoundobject(objects.compounds);
  119.     redisplay_ellipseobject(objects.ellipses);
  120.     redisplay_lineobject(objects.lines);
  121.     redisplay_splineobject(objects.splines);
  122.     canvas_pixwin->pw_pixrect = pw_pixrect;
  123.     canvas_pixwin->pw_prretained = pw_prretained;
  124.     pw_batch_off(canvas_pixwin);
  125.     pointmarker_shown = marker;
  126.     compoundbox_shown = box;
  127.     write_pixrect(fp, bitmap);
  128.     for (t = objects.texts; t != NULL; t = t->next) write_text(fp, t);
  129.     translate_compound(&objects, xmin, ymin);
  130.     fclose(fp);
  131.     pr_destroy(bitmap);
  132. #endif
  133. #else
  134.     put_msg("Sorry, write_bitmap not implemented for X11 yet");
  135. #endif    X11
  136.     }
  137.  
  138. #ifndef    X11
  139. #ifdef AMIGA
  140. #else
  141. write_pixrect(fp, bitmap)
  142. FILE        *fp;
  143. PIXRECT        bitmap;
  144. {
  145.     int        i, j, width, height, shorts_per_row ;
  146.     u_short        *ptr;
  147.  
  148.     width = bitmap->pr_size.x;
  149.     height = bitmap->pr_size.y;
  150.     shorts_per_row = (int) ((width + 15) / 16);
  151.  
  152.     put_msg("Writing . . .");
  153.  
  154.     fprintf(fp, "/* Format_version=1, Width=%d, Height=%d, ",
  155.         width, height);
  156.     fprintf(fp, "Depth=1, Valid_bits_per_item=16\n */\n");
  157.  
  158.     ptr = (u_short*)((MPR_DATA *) bitmap->pr_data)->md_image;
  159.  
  160.     for (i = 0; i < height; i++) {
  161.         fprintf(fp, "\t");
  162.         fprintf(fp, "0x%04X", *ptr++);
  163.         for (j = 1; j < shorts_per_row; j++) fprintf(fp, ",0x%04X", *ptr++);
  164.         if (height-i != 1) fprintf(fp, ","); /* if i is not the last row */
  165.         fprintf(fp, "\n");
  166.         }
  167.  
  168.     put_msg("Done");
  169.     }
  170. #endif AMIGA
  171. #endif    X11
  172.